package com.example.androidxdemo.database

import android.content.Context
import androidx.room.Database
import androidx.room.Room
import androidx.room.RoomDatabase
import androidx.room.TypeConverters
import com.example.androidxdemo.dao.FunctionDao
import com.example.androidxdemo.entity.Function
import com.example.androidxdemo.utilities.DATABASE_NAME
import com.example.androidxdemo.utils.Converters

/**
 * Create by 姚冰
 * on 2020/7/21
 */

@Database(entities = arrayOf(Function::class), version = 1, exportSchema = false)
@TypeConverters(Converters::class)
abstract class AppDatabase : RoomDatabase() {
    abstract fun plantDao(): FunctionDao

    companion object {
        @Volatile private var instance: AppDatabase? = null

        fun getInstance(context: Context): AppDatabase {
            return instance ?: synchronized(this) {
                instance ?: buildDatabase(context).also { instance = it }
            }
        }

        private fun buildDatabase(context: Context): AppDatabase {
            return Room.databaseBuilder(context, AppDatabase::class.java, DATABASE_NAME).build()
        }
    }
}
